package container

import (
	"context"
	"fmt"
	"github.com/Unixeno/islands/cmd/connection"
	"github.com/Unixeno/islands/cmd/utils"
	"github.com/Unixeno/islands/pb"
	"github.com/spf13/cobra"
)

var restartTimeOut = 0

func GetContainerRestartCmd() *cobra.Command {

	containerStopCmd := &cobra.Command{
		Use:   "restart id",
		Short: "Restart one container",
		Args:  cobra.MinimumNArgs(1),
		Run:   containerRestartHandle,
	}
	containerStopCmd.Flags().IntVar(&restartTimeOut, "time", 10, "Seconds to wait for stop before killing the container")
	return containerStopCmd
}

func containerRestartHandle(cmd *cobra.Command, args []string) {
	id := CheckNameOrId(args[0])
	r, err := connection.Client.RestartContainer(context.Background(), &pb.RestartContainerReq{
		Id:      id,
		Timeout: int32(restartTimeOut),
	})

	if err != nil {
		fmt.Printf("Cannot restart the container for reason %v\n", err)
	} else {
		if !utils.PrintError(r) {
			fmt.Println(id)
		}
	}
}
